package com.ruoyi.drug_transfer.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

//import com.ruoyi.pharmacy_drug.domain.PharmacyDrugStock;
//import com.ruoyi.pharmacy_drug.service.IPharmacyDrugStockService;
import com.ruoyi.pharmacy.controller.PharmacyOperateController;
import com.ruoyi.pharmacy.domain.PharmacyOperate;
import com.ruoyi.warehouse.controller.DrugWarehouseOperateController;
import com.ruoyi.warehouse.domain.DrugWarehouseOperate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.drug_transfer.domain.DrugTransfer;
import com.ruoyi.drug_transfer.service.IDrugTransferService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 药品从药库调到药房Controller
 *
 * @author ruoyi
 * @date 2025-08-22
 */
@RestController
@RequestMapping("/drug_transfer/drug_transfer")

public class DrugTransferController extends BaseController {
    @Autowired
    private IDrugTransferService drugTransferService;


    @Autowired
    private DrugWarehouseOperateController drugWarehouseOperateController;

    @Autowired
    private PharmacyOperateController  pharmacyOperateController;

    /**
     * 查询药品从药库调到药房列表
     */
    @PreAuthorize("@ss.hasPermi('drug_transfer:drug_transfer:list')")
    @GetMapping("/list")
    public TableDataInfo list(DrugTransfer drugTransfer) {
        startPage();
        List<DrugTransfer> list = drugTransferService.selectDrugTransferList(drugTransfer);
        return getDataTable(list);
    }

    /**
     * 导出药品从药库调到药房列表
     */
    @PreAuthorize("@ss.hasPermi('drug_transfer:drug_transfer:export')")
    @Log(title = "药品从药库调到药房", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DrugTransfer drugTransfer) {
        List<DrugTransfer> list = drugTransferService.selectDrugTransferList(drugTransfer);
        ExcelUtil<DrugTransfer> util = new ExcelUtil<DrugTransfer>(DrugTransfer.class);
        util.exportExcel(response, list, "药品从药库调到药房数据");
    }

    /**
     * 获取药品从药库调到药房详细信息
     */
    @PreAuthorize("@ss.hasPermi('drug_transfer:drug_transfer:query')")
    @GetMapping(value = "/{transferId}")

    public AjaxResult getInfo(@PathVariable("transferId") Long transferId) {
        return success(drugTransferService.selectDrugTransferByTransferId(transferId));
    }

    /**
     * 新增药品从药库调到药房
     */
    @PreAuthorize("@ss.hasPermi('drug_transfer:drug_transfer:add')")
    @Log(title = "药品从药库调到药房", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DrugTransfer drugTransfer) {
        // 新增调拨记录（执行后会自动回填自增的调拨ID）
        drugTransferService.insertDrugTransfer(drugTransfer);

        // 获取并打印关键参数
        Long transferId = drugTransfer.getTransferId(); // 调拨ID（自增主键）
        Long pharmacyId = drugTransfer.getPharmacyId(); // 药房ID
        Long drugId = drugTransfer.getDrugId();         // 药品ID
        Long quantity = drugTransfer.getQuantity();     // 调拨数量
        Long warehouseId = drugTransfer.getWarehouseId(); // 药库ID
        String remark = drugTransfer.getRemark();
        // 打印参数值
        System.out.println("===== 调拨参数信息 =====");
        System.out.println("调拨ID: " + transferId);
        System.out.println("药房ID: " + pharmacyId);
        System.out.println("药品ID: " + drugId);
        System.out.println("调拨数量: " + quantity);
        System.out.println("药库ID: " + warehouseId);
        System.out.println("备注: " + (remark != null ? remark : "无"));
        System.out.println("=======================");

        DrugWarehouseOperate drugWarehouseOperate = new DrugWarehouseOperate();
        //调用药库更新操作，设置实体类，传参
        drugWarehouseOperate.setDrugId(drugId);
        drugWarehouseOperate.setWarehouseId(warehouseId);
        drugWarehouseOperate.setQuantity(quantity);
        drugWarehouseOperate.setRemark(remark);
        drugWarehouseOperate.setOperateType(2);
        // 关键：若 edit 方法需要 existing operateId，需先新增操作记录并获取ID
        drugWarehouseOperateController.add(drugWarehouseOperate);

        PharmacyOperate pharmacyOperate = new PharmacyOperate();
        //设置药房添加操作参数
        pharmacyOperate.setDrugId(Math.toIntExact(drugId));
        pharmacyOperate.setPharmacyId(Math.toIntExact(pharmacyId));
        pharmacyOperate.setQuantity(Math.toIntExact(quantity));
        pharmacyOperate.setRemark(remark);
        pharmacyOperate.setOperateType(1);
        pharmacyOperate.setRelatedId(Math.toIntExact(transferId));
        pharmacyOperateController.addOperateAndUpdateStock(pharmacyOperate);

        return AjaxResult.success("调拨成功，库存已更新");
    }

    /**
     * 修改药品从药库调到药房
     */
    @PreAuthorize("@ss.hasPermi('drug_transfer:drug_transfer:edit')")
    @Log(title = "药品从药库调到药房", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DrugTransfer drugTransfer) {
        return toAjax(drugTransferService.updateDrugTransfer(drugTransfer));
    }

    /**
     * 删除药品从药库调到药房
     */
    @PreAuthorize("@ss.hasPermi('drug_transfer:drug_transfer:remove')")
    @Log(title = "药品从药库调到药房", businessType = BusinessType.DELETE)
    @DeleteMapping("/{transferIds}")
    public AjaxResult remove(@PathVariable Long[] transferIds) {
        return toAjax(drugTransferService.deleteDrugTransferByTransferIds(transferIds));
    }
}
